
package net.qiqbframework.messaging.annotation;


import net.qiqbframework.common.resolving.ResolvedAttributes;
import net.qiqbframework.messaging.Message;

import javax.annotation.Nonnull;
import java.lang.annotation.Annotation;
import java.util.Map;
import java.util.Optional;

public abstract class WrappedMessageExecutableHandler implements MessageExecutableHandler {

    private final MessageExecutableHandler delegate;


    protected WrappedMessageExecutableHandler(MessageExecutableHandler delegate) {
        this.delegate = delegate;
    }

    @Override
    public Class<?> payloadType() {
        return delegate.payloadType();
    }

    @Override
    public int priority() {
        return delegate.priority();
    }

    @Override
    public boolean canHandle(@Nonnull Message<?> message) {
        return delegate.canHandle(message);
    }

    @Override
    public Object handle(@Nonnull Message<?> message, Object target) throws Exception {
        return delegate.handle(message, target);
    }



    @SuppressWarnings("unchecked")
    @Override
    public <HT> Optional<HT> unwrap(Class<HT> handlerType) {
        if (handlerType.isInstance(this)) {
            return (Optional<HT>) Optional.of(this);
        } else if (handlerType.isInstance(delegate)) {
            return (Optional<HT>) Optional.of(delegate);
        }
        return delegate.unwrap(handlerType);
    }

    @Override
    public Optional<Map<String, Object>> annotationAttributes(Class<? extends Annotation> annotationType) {
        return delegate.annotationAttributes(annotationType);
    }

    @Override
    public <R> Optional<R> attribute(String attributeKey) {
        return delegate.attribute(attributeKey);
    }

    @Override
    public ResolvedAttributes attributes() {
        return delegate.attributes();
    }
}
